package com.aliothservice.catfish.order;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.aliothservice.catfish.SessionManager;
import com.aliothservice.catfish.order.data.OrderDataManager;
import com.aliothservice.catfish.order.request.BuyerReceivedProductOrderRequest;
import com.aliothservice.catfish.order.request.CompletedProductOrdersRequest;
import com.aliothservice.catfish.order.request.CompletedVehicleOrdersRequest;
import com.aliothservice.catfish.order.request.CompletedWarehouseOrdersRequest;
import com.aliothservice.catfish.order.request.CreateOrderRequest;
import com.aliothservice.catfish.order.request.DeleteOrderRequest;
import com.aliothservice.catfish.order.request.LoadPendingWarehouseOrdersRequest;
import com.aliothservice.catfish.order.request.LoadedWarehouseOrdersRequest;
import com.aliothservice.catfish.order.request.OrderInfoRequest;
import com.aliothservice.catfish.order.request.OrderPaymentRequestRequest;
import com.aliothservice.catfish.order.request.OverallProductOrderCountingRequest;
import com.aliothservice.catfish.order.request.OverallVehicleOrderCountingRequest;
import com.aliothservice.catfish.order.request.OverallWarehouseOrderCountingRequest;
import com.aliothservice.catfish.order.request.ReviewPendingProductOrdersRequest;
import com.aliothservice.catfish.order.request.ReviewPendingVehicleOrdersRequest;
import com.aliothservice.catfish.order.request.ReviewPendingWarehouseOrdersRequest;
import com.aliothservice.catfish.order.request.SellerAssignVehicleToVehicleOrderRequest;
import com.aliothservice.catfish.order.request.SellerLoadedWarehouseOrderRequest;
import com.aliothservice.catfish.order.request.SellerOffloadWarehouseOrderRequest;
import com.aliothservice.catfish.order.request.SellerReviewListingRequest;
import com.aliothservice.catfish.order.request.SellerShippedProductOrderRequest;
import com.aliothservice.catfish.order.request.SellerTransportedVehicleOrderRequest;
import com.aliothservice.catfish.order.request.SellerTransportsVehicleOrderRequest;
import com.aliothservice.catfish.order.request.ShipPendingProductOrdersRequest;
import com.aliothservice.catfish.order.request.ShippedProductOrdersRequest;
import com.aliothservice.catfish.order.request.SubmitAppealRequestRequest;
import com.aliothservice.catfish.order.request.TransactionListingRequest;
import com.aliothservice.catfish.order.request.TransportOnGoingVehicleOrdersRequest;
import com.aliothservice.catfish.order.request.TransportPendingVehicleOrdersRequest;
import com.aliothservice.catfish.order.request.UnpaidProductOrdersRequest;
import com.aliothservice.catfish.order.request.UnpaidVehicleOrdersRequest;
import com.aliothservice.catfish.order.request.UnpaidWarehouseOrdersRequest;
import com.aliothservice.catfish.order.request.WriteProductOrderReviewRequest;
import com.aliothservice.catfish.order.request.WriteVehicleOrderReviewRequest;
import com.aliothservice.catfish.order.request.WriteWarehouseOrderReviewRequest;

public class OrderController {

	public Map<String, String> createOrder(CreateOrderRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// check parameters
		if ((null==req.getVehicle()) && (null==req.getWarehouse()) && (null==req.getProduct())) {
			map.put("errorCode", "400");
			map.put("errorMessage", "empty request");
			return map;
		}
		if (null != req.getWarehouse()) {
			for (Map<String, Object> reqMap : req.getWarehouse()) {
				if ((null==reqMap.get("serviceId")) || (null==reqMap.get("buyerName")) || (null==reqMap.get("buyerPhoneNumber")) || 
					(null==reqMap.get("startDate")) || (null==reqMap.get("untilDate")) || (null==reqMap.get("volume"))) {
					map.put("errorCode", "400");
					map.put("errorMessage", "missing parameters");
					return map;
				}
			}
		}
		if (null != req.getProduct()) {
			for (Map<String, Object> reqMap : req.getProduct()) {
				if ((null==reqMap.get("serviceId")) || (null==reqMap.get("buyerName")) || (null==reqMap.get("buyerPhoneNumber")) ||
					(null==reqMap.get("buyerAddress")) || (null==reqMap.get("volume"))) {
					map.put("errorCode", "400");
					map.put("errorMessage", "missing parameters");
					return map;
				}
			}
		}
		if (null != req.getVehicle()) {
			for (Map<String, Object> reqMap : req.getVehicle()) {
				if ((null==reqMap.get("serviceId")) || (null==reqMap.get("buyerName")) || (null==reqMap.get("buyerPhoneNumber")) ||
					(null==reqMap.get("temperatureSpec")) || (null==reqMap.get("originAddress")) || (null==reqMap.get("destinationAddress")) ||
					(null==reqMap.get("loadingDate"))) {
					map.put("errorCode", "400");
					map.put("errorMessage", "missing parameters");
					return map;
				}
			}
		}
		
		// create order(s)
		logger.info("prepare order for " + phoneNumber);
		if (null != req.getVehicle()) {
			logger.info(phoneNumber + " vehicles: " + req.getVehicle().toString());
		}
		if (null != req.getWarehouse()) {
			logger.info(phoneNumber + " warehouse: " + req.getWarehouse().toString());
		}
		if (null != req.getProduct()) {
			logger.info(phoneNumber + " products: " + req.getProduct().toString());
		}
		
		Map<String, String> res = orderDataManager.createUserOrder(phoneNumber, req);
		if (null == res) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to create");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return res;
	}
	
	public Map<String, String> orderPaymentRequest(OrderPaymentRequestRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// check parameters
		if (null == req.getLocator()) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		if ((0!=req.getLocator().compareTo("vehicle")) && (0!=req.getLocator().compareTo("warehouse")) && (0!=req.getLocator().compareTo("product"))) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// process request
		String url = orderDataManager.orderPaymentRequest(phoneNumber, req);
		if (null == url) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters - invalid state");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		map.put("paymentUrl", url);
		return map;
	}
	
	public Map<String, String> deleteOrder(DeleteOrderRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// check parameters
		if (null == req.getLocator()) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		if ((0!=req.getLocator().compareTo("vehicle")) && (0!=req.getLocator().compareTo("warehouse")) && (0!=req.getLocator().compareTo("product"))) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform deletion
		if (!orderDataManager.deleteOrder(phoneNumber, req)) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to delete");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public void orderPaid(Map<String, String> reqMap) {
		String internalOrderNumber = reqMap.get("out_trade_no");
		String alipayOrderNumber = reqMap.get("trade_no");
		
		orderDataManager.orderPaid(internalOrderNumber, alipayOrderNumber);
	}
	
	public void specificOrderPaid(Map<String, String> reqMap) {
		String internalOrderNumber = reqMap.get("out_trade_no");
		String alipayOrderNumber = reqMap.get("trade_no");

		orderDataManager.specificOrderPaid(internalOrderNumber, alipayOrderNumber);
	}
	
	public Object unpaidWarehouseOrders(UnpaidWarehouseOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.unpaidWarehouseOrders(phoneNumber);
	}
	
	public Object unpaidProductOrders(UnpaidProductOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.unpaidProductOrders(phoneNumber);
	}
	
	public Object unpaidVehicleOrders(UnpaidVehicleOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.unpaidVehicleOrders(phoneNumber);
	}
	
	public Object loadPendingWarehouseOrders(LoadPendingWarehouseOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.loadPendingWarehouseOrders(phoneNumber);
	}
	
	public Object shipPendingProductOrders(ShipPendingProductOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.shipPendingProductOrders(phoneNumber);
	}
	
	public Object transportPendingVehicleOrders(TransportPendingVehicleOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.transportPendingVehicleOrders(phoneNumber);
	}
	
	public Object shippedProductOrders(ShippedProductOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.shippedProductOrders(phoneNumber);
	}
	
	public Object reviewPendingProductOrders(ReviewPendingProductOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.reviewPendingProductOrders(phoneNumber);
	}
	
	public Object completedProductOrders(CompletedProductOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.completedProductOrders(phoneNumber);
	}
	
	public Map<String, String> sellerShippedProductOrder(SellerShippedProductOrderRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if (null == req.getOrderId()) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform shipped operation
		if (!orderDataManager.sellerShippedProductOrder(phoneNumber, req.getOrderId())) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to operate");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Map<String, String> buyerReceivedProductOrder(BuyerReceivedProductOrderRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if (null == req.getOrderId()) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform receiving operation
		if (!orderDataManager.buyerReceivedProductOrder(phoneNumber, req.getOrderId())) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to operate");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Map<String, String> writeProductOrderReview(WriteProductOrderReviewRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if ((null==req.getOrderId()) || (null==req.getStarCount()) || (null==req.getReviewContent())) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform write operation
		if (!orderDataManager.writeProductOrderReview(phoneNumber, req)) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to operate");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Map<String, String> writeWarehouseOrderReview(WriteWarehouseOrderReviewRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if ((null==req.getOrderId()) || (null==req.getStarCount()) || (null==req.getReviewContent())) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform write operation
		if (!orderDataManager.writeWarehouseOrderReview(phoneNumber, req)) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to operate");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Map<String, String> sellerLoadedWarehouseOrder(SellerLoadedWarehouseOrderRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if (null == req.getOrderId()) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform loading
		if (!orderDataManager.sellerLoadedWarehouseOrder(phoneNumber, req.getOrderId())) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to operate");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Object loadedWarehouseOrders(LoadedWarehouseOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.loadedWarehouseOrders(phoneNumber);
	}
	
	public Object reviewPendingWarehouseOrders(ReviewPendingWarehouseOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.reviewPendingWarehouseOrders(phoneNumber);
	}
	
	public Map<String, String> sellerAssignVehicleToVehicleOrder(SellerAssignVehicleToVehicleOrderRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if ((null==req.getOrderId()) || (null==req.getVehicleId()) || (null==req.getDriverId())) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform assign
		if (!orderDataManager.sellerAssignVehicleToVehicleOrder(phoneNumber, req)) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to operate");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Map<String, String> sellerOffloadWarehouseOrder(SellerOffloadWarehouseOrderRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if (null == req.getOrderId()) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform offload
		if (!orderDataManager.sellerOffloadWarehouseOrder(phoneNumber, req.getOrderId())) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to operate");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Map<String, String> sellerTransportsVehicleOrder(SellerTransportsVehicleOrderRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if (null == req.getOrderId()) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform transport
		if (!orderDataManager.sellerTransportsVehicleOrder(phoneNumber, req.getOrderId())) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to operate");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Object transportOnGoingVehicleOrders(TransportOnGoingVehicleOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.transportOnGoingVehicleOrders(phoneNumber);
	}
	
	public Object reviewPendingVehicleOrders(ReviewPendingVehicleOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.reviewPendingVehicleOrders(phoneNumber);
	}
	
	public Object completedVehicleOrders(CompletedVehicleOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.completedVehicleOrders(phoneNumber);
	}
	
	public Object completedWarehouseOrders(CompletedWarehouseOrdersRequest req) {
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.completedWarehouseOrders(phoneNumber);
	}
	
	public Map<String, String> writeVehicleOrderReview(WriteVehicleOrderReviewRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if ((null==req.getOrderId()) || (null==req.getStarCount()) || (null==req.getReviewContent())) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform review
		if (!orderDataManager.writeVehicleOrderReview(phoneNumber, req)) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to operate");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Map<String, String> sellerTransportedVehicleOrder(SellerTransportedVehicleOrderRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if (null == req.getOrderId()) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform transported
		if (!orderDataManager.sellerTransportedVehicleOrder(phoneNumber, req.getOrderId())) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to operate");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Map<String, String> overallProductOrderCounting(OverallProductOrderCountingRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if (null == req.getQ()) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		if ((0!=req.getQ().compareTo("buyer")) && (0!=req.getQ().compareTo("seller"))) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform counting
		orderDataManager.overallProductOrderCounting(phoneNumber, map, req.getQ());
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Map<String, String> overallWarehouseOrderCounting(OverallWarehouseOrderCountingRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if (null == req.getQ()) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		if ((0!=req.getQ().compareTo("buyer")) && (0!=req.getQ().compareTo("seller"))) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform counting
		orderDataManager.overallWarehouseOrderCounting(phoneNumber, map, req.getQ());
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Map<String, String> overallVehicleOrderCounting(OverallVehicleOrderCountingRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if (null == req.getQ()) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		if ((0!=req.getQ().compareTo("buyer")) && (0!=req.getQ().compareTo("seller"))) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform counting
		orderDataManager.overallVehicleOrderCounting(phoneNumber, map, req.getQ());
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Object transactionListing(TransactionListingRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return orderDataManager.transactionListing(phoneNumber);
	}
	
	public Object sellerReviewListing(SellerReviewListingRequest req) {
		Map<String, String> map = new HashMap<String, String>();
				
		// validate parameters
		if ((null==req.getProductId()) && (null==req.getWarehouseId()) && (null==req.getVehicleId())) {
			map.put("errorCode", "400");
			map.put("errorMessage", "empty request");
			return map;
		}
		
		return orderDataManager.sellerReviewListing(req);
	}
	
	public Map<String, String> submitAppealRequest(SubmitAppealRequestRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if ((null==req.getOrderId()) || (null==req.getLocator()) || (null==req.getDescription()) || (null==req.getRefundAmount())) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// perform appeal submission
		if (!orderDataManager.submitAppealRequest(phoneNumber, req)) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to submit");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return map;
	}
	
	public Object orderInfo(OrderInfoRequest req) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(req.getToken());
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if (null == req.getOrderId()) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		// locate user order
		Map<String, Object> res = orderDataManager.orderInfo(phoneNumber, req);
		if (null == res) {
			map.put("errorCode", "500");
			map.put("errorMessage", "order not found");
			return map;
		}
		
		// reset token expiration
		sessionManager.resetRestTokenExpiration(phoneNumber, req.getToken());
		
		return res;
	}
	
	public Map<String, String> markOrderAsPaid(String token, String orderId) {
		Map<String, String> map = new HashMap<String, String>();
		
		// validate token
		String phoneNumber = (String)sessionManager.valueForRestToken(token);
		if (null == phoneNumber) {
			map.put("errorCode", "300");
			map.put("errorMessage", "token expired");
			return map;
		}
		
		// validate parameters
		if (null == orderId) {
			map.put("errorCode", "400");
			map.put("errorMessage", "missing parameters");
			return map;
		}
		
		if (!orderDataManager.markOrderAsPaid(orderId)) {
			map.put("errorCode", "500");
			map.put("errorMessage", "failed to mark as paid");
			return map;
		}
		
		return map;
	}
	
	@Autowired
	private SessionManager sessionManager;
	
	@Autowired
	private OrderDataManager orderDataManager;
		
	private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
	
}
